<template>
  <div class="unitInfo-tableContainer">
    <div class="unitInfo-tableTitle">
      <h2>{{yearText}}年临安区公共机构公务用车油耗情况汇总表</h2>
      <span>
        【 汇总年份：
        <el-date-picker v-model="yearText" type="year" value-format="yyyy" placeholder="选择年" style="width:100px;" @change="getDetail()">
        </el-date-picker> 】
      </span>
    </div>
    <div class="unitInfo-tableContent">
      <table width="100%" border="1" cellpadding="10">
        <tr>
          <td align="center" valign="middle">车号</td>
          <td align="center" valign="middle">车型</td>
          <td align="center" valign="middle">上半年<br/>公里数(千米)</td>
          <td align="center" valign="middle">上半年<br/>油耗(升)</td>
          <td align="center" valign="middle">上半年<br/>百公里油耗(升)</td>
          <td align="center" valign="middle">下半年<br/>公里数(千米)</td>
          <td align="center" valign="middle">下半年<br/>油耗(升)</td>
          <td align="center" valign="middle">下半年<br/>百公里油耗(升)</td>
          <td align="center" valign="middle">备注</td>
        </tr>
        <tr :key="index" v-for="(item ,index) in energyData">
          <td align="center" valign="middle">{{item.Number||'暂无'}}</td>
          <td align="center" valign="middle">{{item.Type||'暂无'}}</td>
          <td align="center" valign="middle">{{item.FirstKilometres||0}}</td>
          <td align="center" valign="middle">{{item.FirstFuelConsumption||0}}</td>
          <td align="center" valign="middle">{{calculate(item.FirstKilometres,item.FirstFuelConsumption)||0}}</td>
          <td align="center" valign="middle">{{item.SecondKilometres||0}}</td>
          <td align="center" valign="middle">{{item.SecondFuelConsumption||0}}</td>
          <td align="center" valign="middle">{{calculate(item.SecondKilometres,item.SecondFuelConsumption)||0}}</td>
          <td align="center" valign="middle"></td>
        </tr>
        <tr v-if="energyData.length==0">
          <td align="center" colspan="9" valign="middle">暂无数据</td>
        </tr>
      </table>
      <div class="table-button">
        <a class="downLoad" :href="exportUrl">导出</a>
        <!--<el-button size="mini">打印</el-button>-->
      </div>
    </div>
  </div>
</template>

<script>
  export default {
    data() {
      return {
        yearText: new Date().getFullYear() + '',
        exportUrl: '',
        energyData: []
      }
    },
    methods: {
      async getDetail() {
        this.exportUrl = `${this.baseUrl}/api/CarFuel/statistics?year=${this.yearText}&departmentName=&export=true`
        this.energyData = []
        const result = await this.$get("/api/CarFuel/statistics", {
          year: this.yearText,
          department: ''
        })
        console.log(result, '车辆油耗汇总')
        if (result.length > 0) {
          this.energyData = result
        }
      },
      calculate(km, oil) {
        if (km && oil) return ((oil / km) * 100).toFixed(2)
        return 0
      }
    },
    created() {
      this.exportUrl = `${this.baseUrl}/api/CarFuel/statistics?year=${this.yearText}&departmentName=&export=true`
      this.showHome.isShow = false;
      this.getDetail()
    }
  }

</script>
<style lang="scss" scoped>
  .unitInfo-tableContainer {
    width: 100%;
    padding: 20px 15px;
  }

  .unitInfo-tableTitle {
    display: flex;
    width: 100%;
    align-items: center;
    flex-direction: column;
    justify-content: center;
    margin-bottom: 25px;
    h2 {
      font-size: 16px;
      margin-bottom: 10px;
    }
    span {
      font-size: 16px;
      font-weight: bold;
    }
    p {
      font-size: 16px;
      margin-top: 5px;
      font-weight: bold;
    }
  }

  .unitInfo-tableInfo {
    display: flex;
    flex-direction: row;
    justify-content: space-between;
    margin-bottom: 25px;
    color: #606266;
  }

  .unitInfo-tableContent {
    width: 100%;
    overflow: auto;
    .table-titles {
      margin-bottom: 15px;
      display: flex;
      flex-direction: row;
      justify-content: flex-start;
      align-items: center;
      span {
        margin-right: 200px;
      }
    }
    table {
      color: #606266;
      border: 1px solid #ebeef5;
      td {
        padding: 6px 0px;
      }
      tr.table-row {
        height: 45px;
      }
    }
  }

  .table-footers {
    margin: 15px 0;
    display: flex;
    flex-direction: column;
    .tableFooter-row {
      display: flex;
      flex-direction: row;
      justify-content: space-between;
      height: 25px;
      .tableFooter-col {
        flex: 1;
      }
    }
  }

  .table-button {
    margin-top: 15px;
    display: flex;
    flex-direction: row;
    justify-content: center;
  }

  a.downLoad {
    display: inline-block;
    border: 1px solid #99D3F5;
    border-radius: 4px;
    padding: 4px 18px;
    color: #1E88C7;
    text-indent: 0;
    font-size: 14px;
    line-height: 20px;
    margin-left: 20px;
  }
</style>